<?php
namespace Home\Controller;

use Home\Controller\PublicController;

header('content-type:text/html;charset=utf-8');

class IndexController extends PublicController
{
    public function login(){
        $this->display();
    }

    public function loginAction(){
        $m=M('users');
        $where['tel']=I('post.tel');//获取电话
        $where['password']=I('post.password');//获取密码
        $res=$m->where($where)->find();//根据条件查询
        if($res && !empty($where['password'])){//判断密码手机好是否正确
           //$message="该账号不存在，请重新输入";
           //$this->success('登陆成功','index');
            session('user_id',$res['id']);//写入session
            session('user_tel',$res['tel']);
            $this->redirect("index");

        }else{

          $this->error('账号或密码错误，请重新输入！','login');
        }
// $arr=$m->getLastSql();//获取最后一次的sql语句
//         echo $arr;    
     }

     public function logout(){//清空session
        session('user_id',null);//写入session
        session('user_tel',null);
        $this->redirect("index");
     }
    public function register(){
        $this->display();
    }

    public function registerAction(){
            
        if(IS_POST) {//判断是否有数据POST进入，没有则不执行
        $m=M('users');
            $condition['tel']=I('post.tel');//
            $condition1['email']= I('post.email');
            $tiaojian['tel']=I('post.tel');
            $tiaojian['email']= I('post.email');
            if(I('post.password')){
                $data['password']=I('post.password');//密码
            }else{
                $data['password']=I('post.apassword');
            }
            $data['tel'] = I('post.tel');
            $data['email'] = I('post.email');
            $data['addtime'] = null;


            $code=I('post.code'); //获取验证码
            $verify=new\Think\Verify();  
            $res=$verify->check($code);//执行函数
            if($res){
      
                if(!$condition['tel']==NULL || !$condition1['email']==NULL){//判断使用手机还是邮箱注册
                    if($condition['tel']==NULL && $condition1['email']==NULL){//手机邮箱都存在时注册失败
                            $result1=$m->where($tiaojian )->select();
                    }
                    $result=$m->where($tiaojian)->select();//查找手机号存在且邮箱为空的用户书否存在，若不存在继续执行
                             if($result){//若存在则报错
                                 $this->error('该账号已存在，请重新输入！');
                              }else{//若不存在
                                 $result1=$m->where($tiaojian )->select();//查找用手机号不存在邮箱存在的用户是否存在,若存在，则报错
                                 if($result1){
                                   $this->error('该账号已存在，请重新输入！');
                                }else{//若不存在则继续执行
                                    $res = $m->data($data)->add();
                                    if($res){
                                        $this->success('注册成功');
                                    }
                                }
                              }
                }else{//如果电话和邮箱都为空注册则会报错
                    $this->error("请不要胡闹亲！！！");
                }
            }else{
                $this->error('验证码错误',"register");
            }                
        }
    }

    public function code(){
        $verify=new\Think\Verify();
        $verify->fontSize=15;
        $verify->length=4;
        $verify->useNoise=false;
        $verify->imageW=110;
        $verify->entry();
    }

    function check_verify($code, $id = ''){ //验证码函数
     $verify = new \Think\Verify();    
     return $verify->check($code, $id);
    }

    
    public function select(){
        //dump(I('post.'));

        $m=M('users');
        $res=$m->where(array('id_card'=>I('post.number')))->select();
        $uid=$res[0]['id'];
        $res2=$m->table('think_orders')->where(array('uid'=>$uid))->select();
        /*$data=1;
        $this->ajaxReturn($data);die;*/
        if($res){
            if($res2){
                $data=1;
                $this->ajaxReturn($data);
            }else{
                $data=0;
                $this->ajaxReturn($data);
           }

        }else{
            $data=0;
            $this->ajaxReturn($data);
        }

    }
}